package com.zjx.reggie.controller;


import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import com.zjx.reggie.common.R;
import com.zjx.reggie.entity.Employee;
import com.zjx.reggie.mapper.EmployeeMapper;
import com.zjx.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    //restful风格，查询用get请求，删除用delete请求，新增用post请求，修改用put请求

    @Resource
    private EmployeeService employeeService;

    @RequestMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(wrapper);
        if(one==null){
            //用户不存在
            return R.error("用户不存在");
        }
        if(!BCrypt.checkpw(employee.getPassword(),one.getPassword())){
            //密码不正确
            return R.error("密码不正确");
        }
        if(one.getStatus()==0){
            //该用户禁用
            return R.error("该用户禁用");
        }
        //登录成功，将员工id存入session，并返回登录成功结果
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }

    @RequestMapping("/logout")
    public R logout(HttpServletRequest request){

        request.getSession().removeAttribute("employee");

        return R.success("退出成功！");
    }

    @PostMapping
    public R save(HttpServletRequest request, @RequestBody  Employee employee){

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());

        String id =(String) request.getSession().getAttribute("employee");

        employee.setCreateUser(id);
        employee.setUpdateUser(id);

        employeeService.save(employee);

        return  R.success("员工添加成功！");
    }

    @RequestMapping("/page")
    public R<Page> page(Integer page ,Integer pageSize,String name){
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);
        qw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,qw);

        return  R.success(pageInfo);
    }

    @PutMapping()
    public R update(HttpServletRequest request,@RequestBody  Employee employee){
        String  id = (String )request.getSession().getAttribute("employee");
        employee.setUpdateUser(id);
        employee.setUpdateTime(new Date());

        employeeService.updateById(employee);
        return  R.success("状态修改成功！");
    }

    @GetMapping("/{id}")
    public R updateById(@PathVariable String  id){
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getId,id);
        Employee user = employeeService.getOne(qw);
        return R.success(user);
    }
}
